package com.aidex.system.event;

import com.aidex.system.service.ApiLogService;
import com.aidex.system.service.RunLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author jmy
 * @version 1.1
 * @date 2021-12-10 22:29
 */
@Component
public class LogEventListener {

    @Autowired
    private RunLogService runLogService;
    @Autowired
    private ApiLogService apiLogService;

    @Async("taskExecutor")
    @EventListener(LogEvent.class)
    public void recordApiLog(LogEvent event) {
        if (event.isSuccess()) {
            apiLogService.updateScuess(event.getApiType());
        } else {
            apiLogService.updateError(event.getApiType());
        }
    }

    @Async("taskExecutor")
    @EventListener(LogEvent.class)
    public void recordRunLog(LogEvent event) {
        if (!event.isSuccess()) {
            runLogService.saveError(event.getApiType(), event.getErrorMsg());
        }
    }

}
